from demo import settings
import os
from celery import Celery

# 设置Django的默认配置模块
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')

# 创建 Celery 应用实例
app = Celery(
    "market",  # 项目名称
    broker=os.getenv("CELERY_BROKER_URL", f"redis://{settings.redis_host}:{settings.redis_port}/{settings.redis_db_for_celery}"),
    backend=os.getenv("CELERY_RESULT_BACKEND", f"redis://{settings.redis_host}:{settings.redis_port}/{settings.redis_db_for_celery_result}")
)

# 加载配置文件
app.conf.update(
    task_serializer="json",
    accept_content=["json"],
    result_serializer="json",
    timezone="Asia/Shanghai",
    enable_utc=True,
    task_default_queue="default",
    task_result_expires=3600,
    task_annotations={
        "tasks.*": {
            "max_retries": 3,
            "default_retry_delay": 30,
        }
    }
)

# 自动发现任务模块从django的settings中加载
app.autodiscover_tasks(settings.INSTALLED_APPS)


